package com.swufe.catchme;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Start extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "catchme";
    private int number;
    private int allscore;
    TextView score;
    TextView time;
    private ImageView cat1;
    private ImageView cat2;
    private ImageView cat3;
    private ImageView cat4;
    private ImageView cat5;
    private ImageView cat6;
    private ImageView cat7;
    private ImageView cat8;
    private ImageView cat9;
    private ImageView cat10;
    private ImageView cat11;
    private ImageView cat12;
    private int[] arrays={1,2,3,4,5,6,7,8,9,10,11,12};
    ArrayList<ImageView>cats;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        SharedPreferences sharedPreferences = getSharedPreferences("myscore", Activity.MODE_PRIVATE);
        allscore = sharedPreferences.getInt("allscore",0);
        Log.i(TAG, "onCreate: sp allscore=" + allscore);

        //将得分写到SP里
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("dollar_rate",allscore);
        editor.commit();
        Log.i(TAG, "onActivityResult: 数据已保存到sharedPreferences");

        allscore=0;
        score = (TextView) findViewById(R.id.showscore);
        score.setText(""+allscore);
        time=(TextView) findViewById(R.id.showtime);

        cat1=(ImageView) findViewById(R.id.cat1);
        cat1.setVisibility(View.INVISIBLE);

        cat2=(ImageView) findViewById(R.id.cat2);
        cat2.setVisibility(View.INVISIBLE);

        cat3=(ImageView) findViewById(R.id.cat3);
        cat3.setVisibility(View.INVISIBLE);

        cat4=(ImageView) findViewById(R.id.cat4);
        cat4.setVisibility(View.INVISIBLE);

        cat5=(ImageView) findViewById(R.id.cat5);
        cat5.setVisibility(View.INVISIBLE);

        cat6=(ImageView) findViewById(R.id.cat6);
        cat6.setVisibility(View.INVISIBLE);;

        cat7=(ImageView) findViewById(R.id.cat7);
        cat7.setVisibility(View.INVISIBLE);

        cat8=(ImageView) findViewById(R.id.cat8);
        cat8.setVisibility(View.INVISIBLE);

        cat9=(ImageView) findViewById(R.id.cat9);
        cat9.setVisibility(View.INVISIBLE);

        cat10=(ImageView) findViewById(R.id.cat10);
        cat10.setVisibility(View.INVISIBLE);

        cat11=(ImageView) findViewById(R.id.cat11);
        cat11.setVisibility(View.INVISIBLE);

        cat12=(ImageView) findViewById(R.id.cat12);
        cat12.setVisibility(View.INVISIBLE);

        cats=new ArrayList<ImageView>();
        for (int i =0;i<arrays.length;i++){
            cats.add(cat1);
            cats.add(cat2);
            cats.add(cat3);
            cats.add(cat4);
            cats.add(cat5);
            cats.add(cat6);
            cats.add(cat7);
            cats.add(cat8);
            cats.add(cat9);
            cats.add(cat10);
            cats.add(cat11);
            cats.add(cat12);
        }
        for (ImageView cat:cats){
            cat.setOnClickListener(this);
        }
      int code = getIntent().getIntExtra("code",1);
        if(code==1){
            new MyCount1(30*1000,500).start();
        }
        if(code==2){
            new MyCount2(30*1000,300).start();
        }
        if(code==3){
            new MyCount3(30*1000,150).start();
        }
    }

    private void Chat(){
        for (int i = 0; i < arrays.length; i++) {
            if (i == number) {
                ImageView cat= cats.get(i);
                cat.setVisibility(View.VISIBLE);
                Log.i(TAG, "handleMessage: cat"+cats.get(i));
            }
        }
    }
    private void miss(){
        for (int i = 0; i < arrays.length; i++) {
            if (i == number) {
                ImageView cat= cats.get(i);
                cat.setVisibility(View.INVISIBLE);
            }
        }
    }
    private void showscore(int inc){
        String oldScore=(String) score.getText();
        allscore=Integer.parseInt(oldScore)+inc;
        score.setText(""+allscore);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        switch (view.getId()) {
            case R.id.cat1:
                if (number == 0) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat2:
                if (number == 1) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat3:
                if (number == 2) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }


            case R.id.cat4:
                if (number == 3) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat5:
                if (number == 4) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat6:
                if (number == 5) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat7:
                if (number == 6) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat8:
                if (number == 7) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat9:
                if (number == 8) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat10:
                if (number == 9) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat11:
                if (number == 10) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }

            case R.id.cat12:
                if (number == 11) {
//                    Toast.makeText(this, "Oh! You catch me！", Toast.LENGTH_SHORT).show();
                    showscore(10);
                    Vibrator vibrator=(Vibrator)getSystemService(Service.VIBRATOR_SERVICE);
                    vibrator.vibrate(new long[]{0,200}, -1);
                }
        }

    }



    private class MyCount1 extends CountDownTimer{
        Intent intent1=new Intent(Start.this,MusicService.class);

        public MyCount1(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }

        @Override
        public void onTick(long millisUntilFinished) {
            long minute= millisUntilFinished/1000%3600/60;//分
            long second= millisUntilFinished/1000%3600%60;//秒
            time.setText(minute+":"+second);
            startService(intent1);
            if(second%1==0){
                miss();
                ChangeUI();
                Chat();
            }

        }

        @Override
        public void onFinish() {
            time.setText("0");
            miss();
            Intent intent=new Intent(Start.this,Score.class);
            intent.putExtra("score",allscore);
            startActivity(intent);
            stopService(intent1);

        }
    }
    private class MyCount2 extends CountDownTimer{
        Intent intent2=new Intent(Start.this,MusicService.class);
        public MyCount2(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            startService(intent2);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            long minute= millisUntilFinished/1000%3600/60;//分
            long second= millisUntilFinished/1000%3600%60;//秒
            time.setText(minute+":"+second);
            if(second%1==0){
                for (int k=0;k<10;k++){
                    miss();
                    ChangeUI();
                    Chat();
                }
            }
        }

        @Override
        public void onFinish() {
            time.setText("0");
            miss();
            Intent intent=new Intent(Start.this,Score.class);
            intent.putExtra("score",allscore);
            startActivity(intent);
            stopService(intent2);
        }
    }
    private class MyCount3 extends CountDownTimer{
        Intent intent3=new Intent(Start.this,MusicService.class);
        public MyCount3(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            startService(intent3);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            long minute= millisUntilFinished/1000%3600/60;//分
            long second= millisUntilFinished/1000%3600%60;//秒
            time.setText(minute+":"+second);
            if(second%1==0){
                miss();
                ChangeUI();
                Chat();
            }

        }

        @Override
        public void onFinish() {
            time.setText("0");
            miss();
            Intent intent=new Intent(Start.this,Score.class);
            intent.putExtra("score",allscore);
            startActivity(intent);
            stopService(intent3);
        }
    }
    private void ChangeUI(){
        number=(int) (Math.random()*11);
        Log.i(TAG, "handleMessage: "+number);

        }

    }


